package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.redis.IUserRedisService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@Api(value = "用户资源",description = "用户资源控制器")
public class UserInfoController {
    @Value("${sms.url}")
    private String url  ;
    @Value("${sms.appkey}")
    private String appkey  ;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserRedisService userRedisService;

    @GetMapping("/get")
    public UserInfo get(String id) {
        return userInfoService.get(id);
    }


    @GetMapping("/checkPhone")
    public Boolean checkPhone(String phone) {
        Boolean byPhone = userInfoService.findByPhone(phone);
        return byPhone;
    }

    /**
     * 用户注册
     * @param phone      电话
     * @param nickname   姓名
     * @param password   密码
     * @param rpassword  确认密码
     * @param verifyCode 短信验证码
     * @return
     */
    @ApiOperation(value = "注册功能",notes = "其实就是新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话号码",name = "phone",dataType = "String",required = true),
            @ApiImplicitParam(value = "昵称",name = "nickname",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true),
            @ApiImplicitParam(value = "确认密码",name = "rpassword",dataType = "String",required = true),
            @ApiImplicitParam(value = "短信验证码",name = "verifyCode",dataType = "String",required = true)
    })
    @PostMapping("/userRegist")
    public JsonResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    /**
     * 发送验证码
     * @param phone 手机号用作拼接redis的key
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public JsonResult sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone,url,appkey);
        return JsonResult.success();
    }

    /**
     * 登录验证
     * @param username  手机号
     * @param password  密码
     * @return
     */
    @PostMapping("/userLogin")
    public JsonResult userLogin(String username,String password) {
        //1.验证账号密码是否正确
        UserInfo userInfo = userInfoService.checkLogin(username, password);
        //2.创建令牌,并将用户令牌传入redis
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        userRedisService.setToken(token,userInfo);
        //3.将token传到客户端
        Map map = new HashMap();
        map.put("token",token);
        map.put("user", userInfo);
        return JsonResult.success(map);
    }

    /**
     * 登陆以后的用户信息获取
     * @param request
     */
    @GetMapping("/currentUser")
    @RequireLogin
    public JsonResult currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo userInfo = userRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }
}

